<template>
  <div class="back">
    <div class="m-login">
      <h3>叩丁狼商品管理系统</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            autocomplete="off"
            :prefix-icon="User"
            placeholder="请输入账号"
            size="small"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <!-- 点击提交, submitForm将输入的账号和密码进行校验 -->
          <!-- 防抖 / 节流 -->
          <el-button type="primary" v-debounce="() => submitForm(ruleFormRef)"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import {
  ElButton,
  ElForm,
  ElFormItem,
  ElInput,
  FormInstance,
} from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { loginAPI } from './api';
import { useRouter } from 'vue-router';

const router = useRouter();
console.log(router.getRoutes());

// 表单DOM
const ruleFormRef = ref<FormInstance>();
// 密码校验 函数
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else {
    callback();
  }
};
// 账号和密码绑定v-model
const ruleForm = reactive({
  username: 'admin',
  password: '123456',
});
// rules: 校验规则
const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }, //触发校验 提示信息
  ],
  // 密码校验, 验证函数:validatePass
  password: [{ validator: validatePass, trigger: 'blur' }],
});
// 提交按钮
const submitForm = (formEl: FormInstance | undefined) => {
  // formEl 对接收到的参数进行校验
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // 如果valid为true, 提交校验通过, 发起请求
      loginAPI(ruleForm).then((res) => {
        if (res.code === 200) {
          // 1. 本地存储token
          localStorage.setItem('token', res.data.tokenHead + res.data.token);
          // 2. router.push
          router.push('/index');
          // 3. 信息提示
          console.log(1);

          ElMessage.success('登录成功');
        }
      });
    } else {
      ElMessage('输入不匹配'); //如果校验失败, 进行提示并返回false
      return false;
    }
  });
};
</script>

<style scoped lang="less">
.back {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
}
.m-login {
  width: 348px;
  margin: 278px auto 0;
  h3 {
    text-align: center;
    color: #fff;
    padding: 10px 0;
  }
  :deep(.el-input__wrapper) {
    height: 39px;
    box-sizing: border-box;
    background-color: #283443;
    ::placeholder {
      color: #aaa;
    }
    .el-input__inner {
      color: #ddd;
    }
    .el-input__icon {
      font-size: 17px;
    }
  }
  :deep(.el-button--primary) {
    width: 100%;
  }
}
</style>
